require(['./config'],()=>{
    require(['template','request','jquery','zoom'],(template,req)=>{
        class Details{
            constructor(){
                this.count = 1
                this.init()
                this.getNavData()
                this.gwc()
            }
            init(){
                const id = window.location.search.split('=')[1]
                this.getData(id)
                this.getData1(id)
            }
            getData(id){
                req.getDetailsData(id)
                .then(res=>{
                    // console.log(res)
                    if(res.code === 200){
                        this.render1(res.data.detail)
                    }
                })
            }
            getData1(id){
                req.getListData1(id)
                .then(res=>{
                    // console.log(res)
                    if(res.code === 200){
                        this.rend(res.data.items.list)
                    }
                })
            }
            render1(obj){
                const {id ,image, photo,title,price,descContentList}=obj
                const html = template('templateDetails',{id,image,photo,title,price})
                $('.minCont').html(html)
                $('.xq').html(template('templateDetailsDesc',{list:descContentList}))
                this.addReduce()
                this.addTocar({id,image,title,price})
                this.elevateZoom()
            }
            rend(res){
                const list =  res.splice(1,4)
                console.log(list)
                const html = template('temp',{list})
                // console.log(html)
                $('.wds ul').html(html)
            }
            getNavData(){
                req.getNav()
                .then(res =>{
                    if(res.code === 200){
                            this.render(res.data.list)
                    }
                })
            }
            render(res){
                // console.log(res)
                // console.log($('.con ul'))
                const list = res.splice(1,7)
                const html = template('templateNav',{list})
                $('.con ul').html(html)
            }
            addReduce(){
                $('.jian').click(()=>{
                    this.count--
                    if(this.count <=1){
                        this.count = 1
                    } 
                    $('.inp').val(this.count)
                })
                $('.ja').click(()=>{
                    this.count++
                    $('.inp').val(this.count)
                })
            }
            addTocar(obj){
                const {id,image,title,price}=obj
                $('.joincar').click(()=>{
                    let prodList = localStorage.getItem('prodList')
                    if(prodList){
                        prodList = JSON.parse(prodList)
                        const flag = prodList.some(item =>{
                            return item.id === id
                        })
                        if(flag){
                            prodList.forEach(item =>{
                                if(item.id ===id){
                                    item.count += this.count
                                }
                            })
                        }else{
                            prodList.push({id,image,title,price,count:this.count,isChecked:true})
                        }
                        localStorage.setItem('prodList',JSON.stringify(prodList))
                    }else{
                        localStorage.setItem('prodList',JSON.stringify([{id,image,title,price,count:this.count,isChecked:true}]))
                    }
                    $('.loading').fadeIn(1000,()=>{
                        $('.loading').fadeOut(1000)
                    })
                    this.gwc()
                })
                
            }
            gwc(){
                this.count = 0
                let prodList = localStorage.getItem('prodList')
                if(prodList){
                    prodList = JSON.parse(prodList)
                    prodList.forEach(item =>{
                        this.count += item.count
                    })
                }else{}
               $('.gwc').text(this.count)
               this.count = 1
            }
            //放大镜
            elevateZoom(){ 
                $('.zoom-image').elevateZoom({
                    gallery:'minCont'
                })
            }
        }
        return new Details()
    })
})
// let n = $('.nu').text()
// $('.ja').click(function(){
//     n++
//     $('.nu').text(n)
// })
// $('.jian').click(function(){
//     if(n ==1){
//         return false
//     }
//     n--
//     $('.nu').text(n)
// })